<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise.all方法的使用</title>
</head>

<body>
    <ul></ul>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        // Promise.all的使用：将多个promise合并成一个promise，所有子promise为成功状态则执行then(),否则catch()
        // 需求：同一时间展示四个城市的天气

        // 1.请求城市天气，得到promise对象
        const beijingPromise = axios({ url: 'http://hmajax.itheima.net/api/weather', params: { city: '110100' } })
        const shanghaiPromise = axios({ url: 'http://hmajax.itheima.net/api/weather', params: { city: '310100' } })
        const guangzhouPromise = axios({ url: 'http://hmajax.itheima.net/api/weather', params: { city: '440100' } })
        const shenzhenPromise = axios({ url: 'http://hmajax.itheima.net/api/weather', params: { city: '440300' } })

        // 2.使用promise.all ，合并多个promis对象
        const promise = Promise.all([beijingPromise, shanghaiPromise, guangzhouPromise, shenzhenPromise])
        promise.then(result => {
            // 结果数组的顺序和合并时的一致
            document.querySelector('ul').innerHTML = result.map(item => {
                return `<li>城市：${item.data.data.area}----天气：${item.data.data.weather}</li>`
            }).join('')
        }).catch(error => {
            console.dir(error)
        })
    </script>
</body>

</html>